package com.board.interceptor;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings("serial")
public class ExceptionLoggingInterceptor extends AbstractInterceptor {
	Logger log = Logger.getLogger(this.getClass());

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String result = null;
		try {
			result = invocation.invoke();
		} catch (Exception e) {
			log.error(e.getMessage());
			log.error(getErrorMessage(e.getStackTrace()));
			throw e;
		}

		return result;
	}

	private String getErrorMessage(StackTraceElement[] traceElements) {
		StringBuffer result = new StringBuffer();

		for (StackTraceElement element : traceElements) {
			result.append("\n").append(element.toString());
		}
		return result.toString();

	}
}
